package com.bcgtgjyb.huanwen.customview.mylibrary.view58;

import com.bcgtgjyb.huanwen.customview.mylibrary.ResourceTable;
import com.bcgtgjyb.huanwen.customview.mylibrary.tool.ScreenUtil;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by bigwen on 2016/3/2.
 */
public class Loding58View extends DirectionalLayout implements Component.BindStateChangedListener {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private Context mContext;
    private Loading58Path view;
    private int dropHeight = 0;
    private String TAG = Loding58View.class.getName();
    private boolean isPlay = false;
    private AnimatorGroup riseSet;
    private AnimatorGroup dropSet;
    private AnimatorGroup rolation90,rolation120,rolation180;
    private AnimatorGroup aList1,aList2,aList3,aList4;
    private int duration = 600;
    private long time = 0L;
    private AnimatorGroup riseSet1,riseSet2,riseSet3,dropSet1,dropSet2,dropSet3,dropSet4;

    /**
     * 构造
     *
     * @param context 上下文
     */
    public Loding58View(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public Loding58View(Context context, AttrSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        setBindStateChangedListener(this);
        Component parse = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_loding58view, this, true);
        dropHeight = ScreenUtil.dip2px(mContext, 50);
        view = (Loading58Path) parse.findComponentById(ResourceTable.Id_loading58);
        initAnimList();
    }

    private void animStart() {
        view.start();

        aList1.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                dropSet1.start();
                view.nextPath();
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });

        dropSet1.setStateChangedListener(new AnimEnd() {
            @Override
            public void onEnd(Animator animator) {
                aList2.start();
            }
        });

        aList2.setStateChangedListener(new AnimEnd() {
            @Override
            public void onEnd(Animator animator) {
                dropSet2.start();
                view.nextPath();
            }
        });

        dropSet2.setStateChangedListener(new AnimEnd() {
            @Override
            public void onEnd(Animator animator) {
                aList3.start();
            }
        });

        aList3.setStateChangedListener(new AnimEnd() {
            @Override
            public void onEnd(Animator animator) {
                dropSet3.start();
                view.nextPath();
            }
        });
        dropSet3.setStateChangedListener(new AnimEnd() {
            @Override
            public void onEnd(Animator animator) {
                aList1.start();
                view.start();
            }
        });
        aList1.start();
    }

    private void initAnimList() {
        dropSet = dropVA(0, dropHeight);
        dropSet1 = dropVA(0, dropHeight);
        dropSet2 = dropVA(0, dropHeight);
        dropSet3 = dropVA(0, dropHeight);
        aList1 = new AnimatorGroup();
        rolation90 = rotation(360);
        riseSet1 = riseVA(0, -dropHeight);
        aList1.runParallel(riseSet1, rolation90);
        aList1.setDuration(duration);

        aList2 = new AnimatorGroup();
        rolation120 = rotation(90);
        riseSet2 = riseVA(0, -dropHeight);
        aList2.runParallel(riseSet2, rolation120);
        aList2.setDuration(duration);

        aList3 = new AnimatorGroup();
        rolation180 = rotation(180);
        riseSet3 = riseVA(0, -dropHeight);
        aList3.runParallel(riseSet3, rolation180);
        aList3.setDuration(duration);
    }

    private AnimatorGroup riseVA(float x, float group) {
        AnimatorGroup animatorSet = new AnimatorGroup();

        AnimatorProperty property = new AnimatorProperty(view);
        property.moveFromY(Math.abs(group));
        property.moveToY(0);
        animatorSet.runSerially(property);

        // setDuration在isPlay后面设置
        animatorSet.setDuration(500);
        animatorSet.setCurveType(Animator.CurveType.DECELERATE);
        return animatorSet;
    }

    private AnimatorGroup dropVA(float x, float y) {
        AnimatorGroup animatorSet = new AnimatorGroup();
        AnimatorProperty p2 = new AnimatorProperty(view);
        p2.moveFromY(0);
        p2.moveToY(200);
        animatorSet.runSerially(p2);

        // setDuration在isPlay后面设置
        animatorSet.setDuration(duration);
        animatorSet.setCurveType(Animator.CurveType.ACCELERATE);
        return animatorSet;
    }

    private AnimatorGroup rotation(float param) {
        AnimatorGroup animatorSet = new AnimatorGroup();
        AnimatorProperty animatorProperty = new AnimatorProperty(view);
        animatorProperty.rotate(param);
        animatorSet.runSerially(
                animatorProperty
        );
        animatorSet.setDuration(duration);
        animatorSet.setCurveType(Animator.CurveType.DECELERATE);
        return animatorSet;
    }

    /**
     * 开始
     */
    public void start() {
        isPlay = true;
        animStart();
    }

    /**
     * 停止
     */
    public void stop() {
        isPlay = false;
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        start();
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        stop();
    }

    private class AnimEnd implements Animator.StateChangedListener {
        @Override
        public void onStart(Animator animator) {
        }

        @Override
        public void onStop(Animator animator) {
        }

        @Override
        public void onCancel(Animator animator) {
        }

        @Override
        public void onEnd(Animator animator) {
        }

        @Override
        public void onPause(Animator animator) {
        }

        @Override
        public void onResume(Animator animator) {
        }
    }
}
